# -*- coding:utf-8 -*-
# @FileName  :t1t18t24.py
# @Time      :2021/5/17 12:29
import datetime
from multiprocessing import Pool
from urllib import parse
from save_mysql_aicaigou import mysql_checking
import pandas as pd
import requests
from fake_useragent import UserAgent
import time
import re
from time import sleep
import json
from lxml import html
from sql_ac.sql_1 import insert_one, select_all, insert_all
import sys

sys.setrecursionlimit(1000000)
etree = html.etree

ua = UserAgent()
kv = {
    'user-agent': ua.random,
    "Connection": 'close',
    'cookie':'BIDUPSID=1D1BB89E4109822092F63538BF212A0B; PSTM=1618373808; BAIDUID=1D1BB89E41098220F0AAE19A13D15EE2:FG=1; __yjs_duid=1_8d6fb1349911283e51e25afb4c5675de1618373866311; BDUSS=zZtVC1sNUUxMzNBVkZFR1dpblEtOUdKOTkyQTdqOVBVM1dIMUxvUk5HM052NjFnSUFBQUFBJCQAAAAAAAAAAAEAAAD2vuuauf65~sqlttwAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAM0yhmDNMoZgc; BDUSS_BFESS=zZtVC1sNUUxMzNBVkZFR1dpblEtOUdKOTkyQTdqOVBVM1dIMUxvUk5HM052NjFnSUFBQUFBJCQAAAAAAAAAAAEAAAD2vuuauf65~sqlttwAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAM0yhmDNMoZgc; B2B_AB_FCT2=42511786; H_WISE_SIDS=107313_110085_127969_128699_154619_165936_171509_171565_172643_173016_173184_173293_173617_174179_174447_174662_174665_174682_175407_175535_175553_175668_175678_175755_175796_176130_176158_176328_176342_176346_176487_176615_176678_176766_176902_176996_177086_177224_177280_177381_177407_177558; B2B_CL_HISTORY_WL=%5B%7B%22_q%22%3A%22%5Cu6d0b%5Cu9152%22%2C%22_c%22%3A%22%5Cu98df%5Cu54c1%5Cu751f%5Cu9c9c%3B%5Cu9152%5Cu7c7b%3B%5Cu6d0b%5Cu9152%22%2C%22_k%22%3A%2248b9d7bf794f3fd0382dc977c651eef0%22%2C%22_t%22%3A1623378623%7D%5D; MCITY=-340%3A; BDORZ=B490B5EBF6F3CD402E515D22BCDA1598; B2B_AB_SID=201_260_263; BAIDUID_BFESS=1D1BB89E41098220F0AAE19A13D15EE2:FG=1; Hm_lvt_77fe88a330b395c39e37e1ea1cea9a8c=1623485790,1623728020,1623895341,1623897184; B2B_SE_HISTORY_WL=%5B%7B%22_w%22%3A%22%5Cu5973%5Cu88c5%22%2C%22_t%22%3A1623897663%7D%2C%7B%22_w%22%3A%22%5Cu94c5%5Cu7b14%22%2C%22_t%22%3A1623741613%7D%2C%7B%22_w%22%3A%22%5Cu822a%5Cu661f%5Cu6d17%5Cu6da4%5Cu673a%5Cu68b0%28%5Cu6cf0%5Cu5dde%29%5Cu6709%5Cu9650%5Cu516c%5Cu53f8%22%2C%22_t%22%3A1623739944%7D%2C%7B%22_w%22%3A%22%5Cu6d17%5Cu8863%5Cu673a%22%2C%22_t%22%3A1623739870%7D%2C%7B%22_w%22%3A%22%5Cu6f4d%5Cu574a%5Cu8fc5%5Cu641c%5Cu7f51%5Cu7edc%5Cu6280%5Cu672f%5Cu6709%5Cu9650%5Cu516c%5Cu53f8%22%2C%22_t%22%3A1623739648%7D%2C%7B%22_w%22%3A%22%5Cu8033%5Cu673a%22%2C%22_t%22%3A1623728017%7D%2C%7B%22_w%22%3A%22%5Cu5316%5Cu5986%5Cu54c1%22%2C%22_t%22%3A1623489641%7D%2C%7B%22_w%22%3A%22%5Cu5361%5Cu76d2%22%2C%22_t%22%3A1623489430%7D%2C%7B%22_w%22%3A%22%5Cu7259%5Cu818f%22%2C%22_t%22%3A1623487400%7D%2C%7B%22_w%22%3A%22%5Cu6d0b%5Cu9152%22%2C%22_t%22%3A1623378619%7D%5D; H_PS_PSSID=34099_33968_33848_33607_26350; delPer=0; PSINO=6; BA_HECTOR=a021242420a42g8hrq1gcle1t0q; Hm_lpvt_77fe88a330b395c39e37e1ea1cea9a8c=1623898223; ab_sr=1.0.1_N2IwZGU3MTEyYTczYjhiYzQ0YzFmYzI5NjhhMTE5YjJhYWMxZjNhNmM2ZWI4M2YwZTYyYTI5YzI2NzY2MDVjNjMyMmZmM2FkMmM1N2NiOWIyYjc1OTk0Y2Q0MzFjMTZhNjBkYzczZTJhNGNlZjUyM2ExYTUwMGVhNmQxMTQ3NTZjMDAxMjlkYjJiZTliODg1OGQzODUzYWNmZmZiNDk5OA=='
}

tunnel = ""
# 用户名密码方式
username = ""
password = ""
proxie = {
    "http": "http://%(user)s:%(pwd)s@%(proxy)s/" % {"user": username, "pwd": password, "proxy": tunnel},
    "https": "https://%(user)s:%(pwd)s@%(proxy)s/" % {"user": username, "pwd": password, "proxy": tunnel}
}
def shaixuan(p):
    if str(p) == 'None':
        return ''
    else:
        return p


def main(jk, key):
    req = requests.get(
        "https://b2b.baidu.com/c/a?ajax=1&csrf_token=e5f1490abb0055e275182dbe6fbab0f4&logid=255489722421532640&fid=0%2C1623485789541&_=1623487266617&q=" + str(
            key) + "&p=" + str(
            jk) + "&s=15",
        headers=kv).text
    r = json.loads(req)
    print(key, '页数' + str(jk))
    if r['data']['dispNum'] == 0:
        return 0
    for o in r['data']['entList']:
        print(o['title'])
        # print(o['address'])
        # print(i['describe']['value'])
        na = parse.quote(o['title'])
        req1 = requests.get("https://b2b.baidu.com/shop?name=" + na + "&tpath=contact",headers=kv).text
        data = json.loads(re.findall(r'(window.data)(.*)', req1)[0][1][3:-1])
        if shaixuan(data['cardInfo']['phoneNumber']) == '':
            print('没号码')
            break
        # print(data['entInfo']['address'])
        # print(data['entInfo']['province'])
        # print(data['entInfo']['city'])
        # print(data['entInfo']['scope'])
        print(data['cardInfo']['contactName'])
        print(data['cardInfo']['phoneNumber'])
        if mysql_checking(data['cardInfo']['phoneNumber']):
            insert_all('baidu_aicaigou_spider',
                       shaixuan(data['cardInfo']['contactName']).replace("'", "").replace("'", ""),
                       '',
                       data['cardInfo']['phoneNumber'], key,
                       o['title'], '', 12,
                       '0', 1, '', '', '', '', '', '', 2, '', '',
                       shaixuan(data['entInfo']['address']).replace("'", "").replace("'", ""), '', '', '',
                       shaixuan(data['entInfo']['province']), shaixuan(data['entInfo']['city']), '', 0, '',
                       0, '', 0, 0,
                       0, 0, 0, str(time.strftime('%Y-%m-%d %H:%M:%S')), str(time.strftime('%Y-%m-%d %H:%M:%S')),
                       shaixuan(data['entInfo']['scope']).replace("'", "").replace("'", ""), 0,
                       0, '', 0)
        # input('')
        print('\n')


def url_list(jk,key):
    pool = Pool(5)
    for i in range(1, jk+1):
        pool.apply_async(main, (i,key))
    pool.close()
    pool.join()


if __name__ == "__main__":
    for i in select_all('merberid'):
        reqs = requests.get(
            "https://b2b.baidu.com/c/a?ajax=1&csrf_token=e5f1490abb0055e275182dbe6fbab0f4&logid=255489722421532640&fid=0%2C1623485789541&_=1623487266617&q=" + str(
                i[1]) + "&p=1&s=15",
            headers=kv).text
        r = json.loads(reqs)
        print(
            "https://b2b.baidu.com/c/a?ajax=1&csrf_token=e5f1490abb0055e275182dbe6fbab0f4&logid=255489722421532640&fid=0%2C1623485789541&_=1623487266617&q=" + str(
                i[1]) + "&p=1&s=15")
        if r['data']['dispNum'] == 0:
            continue
        # main((r['data']['dispNum'] // 15) + 1, i[1])
        url_list((r['data']['dispNum'] // 15) + 1, i[1])
